package com.toutoushidao.ssm.util;

import com.alibaba.fastjson.JSON;
import com.github.ontio.OntSdk;
import com.github.ontio.account.Account;
import com.github.ontio.common.Address;
import com.github.ontio.common.Helper;
import com.github.ontio.crypto.SignatureScheme;
import com.github.ontio.smartcontract.neovm.abi.AbiFunction;
import com.github.ontio.smartcontract.neovm.abi.AbiInfo;

import java.math.BigDecimal;

public class openboxTest {

    private static final String DAPP_API = "http://polaris1.ont.io";
    private static final String REST_PORT = "20334";
    private static final String NEW_HASH = "6d062d85983eac16cf66a8b4e9951ff7e90bef99";// 新的合约HASH
    private static final String PRIVATE_KEY = "Ky4wEkMWoDayW3naewdu4scAv3ofnEJotZkF9uxrQeYE665jsc1s";
    private static final String PARAM_CONSTRUCTOR = "{\"hash\":\"" + NEW_HASH + "\",\"entrypoint\":\"Main\",\"functions\":[" +
            "{\"name\":\"openBox\",\"parameters\":[{\"name\":\"account\",\"type\":\"ByteArray\"}],\"returntype\":\"Any\"}," +
            "{\"name\":\"withDraw\",\"parameters\":[{\"name\":\"account\",\"type\":\"ByteArray\"},{\"name\":\"amount\",\"type\":\"Integer\"}" +
            "],\"returntype\":\"Any\"}],\"events\":[]}";

    public static void main(String[] s) throws Exception {
        String address = "AZowJCqUiitvXx4LoqR3NwYvu43R6MnjBF";
        String restUrl = DAPP_API + ":" + REST_PORT;
        OntSdk ontSdk = OntSdk.getInstance();
        ontSdk.setRestful(restUrl);
        ontSdk.setDefaultConnect(ontSdk.getRestful());
        AbiInfo abiinfo = JSON.parseObject(PARAM_CONSTRUCTOR, AbiInfo.class);
        String name = "openBox";
        AbiFunction func = abiinfo.getFunction(name);
        func.name = name;
        func.setParamsValue(Address.toScriptHash(Address.decodeBase58(address).toArray()).toArray());
        String  privateHex = Helper.toHexString(Account.getPrivateKeyFromWIF(PRIVATE_KEY));
        Account devAccount = new Account(Helper.hexToBytes(privateHex), SignatureScheme.SHA256WITHECDSA);
        Object obj = ontSdk.neovm().sendTransaction(Helper.reverse(NEW_HASH), devAccount, devAccount, 20000, 500, func, false);
        System.out.println("openBox 开箱子返回数据:" + obj);
    }
}
